package com.servlets;

import com.domain.Movies;
import com.domain.service.MoviesService;
import com.domain.utils.GetConnection;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteMovieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        String movieName = request.getParameter("movieName");

        MoviesService moviesService = new MoviesService();

        List<Movies> moviesList = moviesService.getAll();

        Movies movies = null;

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {

            connection = GetConnection.setConnection();
            preparedStatement = connection.prepareStatement("DELETE FROM MOVIES WHERE MOVIENAME=?");

            preparedStatement.setString(1, movieName);
            preparedStatement.executeUpdate();
            
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        }finally{
            try {
                connection.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            try {
                preparedStatement.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
        }

        response.sendRedirect("MoviesListServlet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
